<template>
  <div class="login-panel">
    <h1 class="title">后台管理系统</h1>

    <div class="tabs">
      <el-tabs type="border-card" stretch v-model="panelName">
        <el-tab-pane>
          <template #label>
            <span class="custom-tabs-label">
              <el-icon><User /></el-icon>
              <span class="label-text">账号登录</span>
            </span>
          </template>
          <!-- 账号登录组件 -->
          <PaneAccount ref="accountRef" />
        </el-tab-pane>
        <el-tab-pane>
          <template #label>
            <span class="custom-tabs-label">
              <el-icon><Iphone /></el-icon>
              <span class="label-text">手机登录</span>
            </span>
          </template>
          <!-- 手机登录组件 -->
          <PanePhone />
        </el-tab-pane>
      </el-tabs>
    </div>

    <div class="controls">
      <el-checkbox v-model="isRememberPass" label="记住密码" size="large" />
      <el-link type="primary">忘记密码</el-link>
    </div>

    <el-button class="login-btn" type="primary" size="large" @click="login">立即登录</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import type { FormInstance } from 'element-plus'
import PaneAccount from './PaneAccount.vue'
import PanePhone from './PanePhone.vue'
import { localCache } from '@/utils/cache'

const panelName = ref('0') // 0：账号登录   1：手机登录
const accountRef = ref<InstanceType<typeof PaneAccount>>() // 定义子组件的类型，这个可以是固定的写法

function login() {
  if (panelName.value === '0') {
    accountRef.value.loginAction(isRememberPass.value)
  } else {
    console.log('手机登录')
  }
}
const CACHE_ISREMPASSWORD = 'isRemPassword'
const isRememberPass = ref<boolean>(localCache.getCache(CACHE_ISREMPASSWORD) ?? false) // 这种写法注意一下

watch(
  () => isRememberPass.value,
  (newVal) => {
    console.log('是否记住密码', newVal)
    localCache.setCache(CACHE_ISREMPASSWORD, newVal)
  },
)
</script>

<style scoped lang="less">
.login-panel {
  width: 330px;
  margin-bottom: 150px;
  text-align: center;
  .title {
    color: #fff;
  }
  .tabs {
    background-color: red;
    margin-top: 20px;
    .custom-tabs-label {
      display: flex;
      align-items: center;
      .label-text {
        margin-left: 5px;
      }
    }
  }
  .controls {
    margin-top: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .login-btn {
    width: 100%;
    margin-top: 10px;
  }
}
</style>
